package org.streets.workflow.engine.task;

import java.sql.Date;
import java.util.List;

import org.streets.workflow.engine.IProcessDefinition;
import org.streets.workflow.engine.IProcessInstance;
import org.streets.workflow.engine.ITaskInstance;
import org.streets.workflow.engine.WorkflowEngine;
import org.streets.workflow.engine.WorkflowException;
import org.streets.workflow.engine.entities.TaskInstance;
import org.streets.workflow.engine.persistence.IWorkflowDAO;
import org.streets.workflow.model.WFDataField;
import org.streets.workflow.model.WFProcess;
import org.streets.workflow.model.WFSubflowTask;
import org.streets.workflow.model.WFTask;
import org.streets.workflow.model.resource.WFSubProcessRef;



/**
 * 
 */
public class DefaultSubflowTaskHandler implements ITaskHandler {

    /* (non-Javadoc)
     * @see org.fireflow.engine.taskinstance.ITaskInstanceRunner#run(org.fireflow.engine.IWorkflowSession, org.fireflow.engine.RuntimeContext, org.fireflow.engine.IProcessInstance, org.fireflow.engine.ITaskInstance)
     */
    public void handle(WorkflowEngine engine, ITaskInstance task) throws WorkflowException {
        
        if (!WFTask.SUBFLOW.equals(task.getTaskType())) {
            throw new WorkflowException(task.getProcess(),
                    task.getActivityModel(),
                    "DefaultSubflowTaskInstanceRunner：TaskInstance的任务类型错误，只能为SUBFLOW类型");
        }
        
        WFTask taskModel = task.getTaskModel();
        WFSubProcessRef subProcessModelRef = ((WFSubflowTask) taskModel).getSubProcessModel();

        IProcessDefinition subProcessDef = engine.getDefinition(subProcessModelRef.getRefId(), -1);
        
        if (subProcessDef == null) {
            WFProcess parentProcessModel = task.getProcess().getModel();
            throw new WorkflowException(task.getProcess().getId(), parentProcessModel,
                    task.getTaskModelId(),
                    "系统中没有Id为" + subProcessModelRef.getRefId() + "的流程定义");
        }
        
        WFProcess subProcessModel = subProcessDef.getModel();

        if (subProcessModel == null) {
            WFProcess parentWorkflowProcess = task.getProcess().getModel();
            throw new WorkflowException(task.getProcess().getId(), parentWorkflowProcess,
                    task.getTaskModelId(),
                    "系统中没有Id为" + subProcessModelRef.getRefId() + "的流程定义");
        }
        
        IWorkflowDAO persistenceService = engine.getRuntimeContext().getPersistenceService();
        //更改任务的状态和开始时间
        ((TaskInstance) task).setState(ITaskInstance.RUNNING);
        ((TaskInstance) task).setStartedTime(new Date(System.currentTimeMillis()));
        persistenceService.saveOrUpdateTask((TaskInstance)task);

        IProcessInstance subProcessInstance = engine.create(subProcessModel.getName(), task);

        //初始化流程变量,从父实例获得初始值
        
        //Map<String ,Object> processVars = ((Task) taskInstance).getProcess().getProcessInstanceVariables();
        List<WFDataField> datafields = subProcessModel.getDataFields();
        for (int i = 0; datafields != null && i < datafields.size(); i++) {
            WFDataField df = datafields.get(i);
            Object var_value = task.getProcess().getVariable(df.getName());
            if (var_value != null) {
            	subProcessInstance.setVariable(df.getName(), var_value);
            }
        }
        //dzb 持久流程变量 
        engine.getRuntimeContext().getPersistenceService().saveOrUpdateProcess(subProcessInstance);
        engine.run(subProcessInstance);
    }
}
